package com.hd.tools;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import javax.servlet.http.HttpSession;

import com.hd.pojo.BasePojo;
import com.hd.pojo.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class BaseAdvice {

	@Before("execution(* com.hd.service.impl.*.add*(..))")
	public void befor(JoinPoint jp){
		BasePojo basePojo = null;
		Object[] obj = jp.getArgs();
		if(obj!=null && obj.length>0){
			for (Object object : obj) {
				if(object instanceof BasePojo){
					basePojo = (BasePojo) object;
				}
			}
		}
		if(basePojo!=null){
			HttpSession session = SystemContext.getSession();
			Date date = new Date();
			Instant instant = date.toInstant();
			ZoneId zone = ZoneId.systemDefault();
			LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
			System.out.println("a");
			basePojo.setCreateTime(localDateTime);
		}
	}

	
	/*@Before("execution(* com.hd.service.impl.*.update*(..))")
	public void update(JoinPoint jp){
		BasePojo basePojo = null;
		Object[] obj = jp.getArgs();
		if(obj!=null && obj.length>0){
			for (Object object : obj) {
				if(object instanceof BasePojo){
					basePojo = (BasePojo) object;
				}
			}
		}
		if(basePojo!=null){
			HttpSession session = SystemContext.getSession();
			User user = (User) session.getAttribute(constants.LOGING_USER);
			basePojo.setModifyBy(user.getId());
			basePojo.setModifyDate(new Date());
		}
	}*/
	
}
